package xyz.yeidi.dao.impl;

import xyz.yeidi.dao.api.BusinessDao;
import xyz.yeidi.pojo.Business;
import xyz.yeidi.utils.ConnUtils;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.ArrayList;
import java.util.List;

/**
 * @Author JIP
 * @Date 2021-08-02 16:35
 * @Version 1.0
 * @Describe :
 */
public class BusinessDaoImpl implements BusinessDao {
    @Override
    public List<Business> getAllBusiness() throws Exception {
        String sql = "select businessName,businessAddress,businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,remarks,businessId from business";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = conn.prepareStatement(sql);
        ResultSet rs = null;
        Business b = null;
        rs = ps.executeQuery();
        List<Business> list = new ArrayList<>();
        while(rs.next()){
            b = new Business();
            b.setBusinessName(rs.getString(1));
            b.setBusinessAddress(rs.getString(2));
            b.setBusinessExplain(rs.getString(3));
            b.setBusinessImg(rs.getString(4));
            b.setOrderTypeId(rs.getInt(5));
            b.setStarPrice(rs.getDouble(6));
            b.setDeliveryPrice(rs.getDouble(7));
            b.setRemarks(rs.getString(8));
            b.setBusinessId(rs.getInt(9));
            list.add(b);
        }
        ConnUtils.close(rs,ps);
        return list;
    }

    @Override
    public Business getBusinessByid(Integer id) throws Exception {
        String sql = "select businessName,businessAddress," +
                "businessExplain,businessImg,orderTypeId,starPrice,deliveryPrice,remarks from business where businessId=?";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Business b = null;
        ps = conn.prepareStatement(sql);
        ps.setObject(1,id);
        rs = ps.executeQuery();
        //如果存在下一个才new User 否则会报空指针异常
        if(rs.next()){
            b = new Business();
            b.setBusinessId(id);
            b.setBusinessName(rs.getString(1));
            b.setBusinessAddress(rs.getString(2));
            b.setBusinessExplain(rs.getString(3));
            b.setBusinessImg(rs.getString(4));
            b.setOrderTypeId(rs.getInt(5));
            b.setStarPrice(rs.getDouble(6));
            b.setDeliveryPrice(rs.getDouble(7));
            b.setRemarks(rs.getString(8));
        }
        ConnUtils.close(rs,ps);
        return b;
    }

    @Override
    public List<Business> getBusinessByTypeId(Integer typeId) throws Exception {
        String sql = "select businessName,businessAddress," +
                "businessExplain,businessImg,businessId,starPrice,deliveryPrice,remarks from business where orderTypeId=?";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Business b = null;
        List<Business> list = new ArrayList<>();
        ps = conn.prepareStatement(sql);
        ps.setObject(1,typeId);
        rs = ps.executeQuery();
        //如果存在下一个才new User 否则会报空指针异常
        while(rs.next()){
            b = new Business();
            b.setOrderTypeId(typeId);
            b.setBusinessName(rs.getString(1));
            b.setBusinessAddress(rs.getString(2));
            b.setBusinessExplain(rs.getString(3));
            b.setBusinessImg(rs.getString(4));
            b.setBusinessId(rs.getInt(5));
            b.setStarPrice(rs.getDouble(6));
            b.setDeliveryPrice(rs.getDouble(7));
            b.setRemarks(rs.getString(8));
            list.add(b);
        }
        ConnUtils.close(rs,ps);
        return list;
    }

    @Override
    public List<Business> searchBusinessByName(String businessName) throws Exception {
        String sql = "select businessName,businessAddress," +
                "businessExplain,businessImg,businessId,starPrice,deliveryPrice,remarks,orderTypeId from business where businessName like ?";
        Connection conn = ConnUtils.getConnection();
        PreparedStatement ps = null;
        ResultSet rs = null;
        Business b = null;
        List<Business> list = new ArrayList<>();
        ps = conn.prepareStatement(sql);
        ps.setObject(1,"%"+businessName+"%");
        rs = ps.executeQuery();
        //如果存在下一个才new User 否则会报空指针异常
        while(rs.next()){
            b = new Business();
            b.setBusinessName(rs.getString(1));
            b.setBusinessAddress(rs.getString(2));
            b.setBusinessExplain(rs.getString(3));
            b.setBusinessImg(rs.getString(4));
            b.setBusinessId(rs.getInt(5));
            b.setStarPrice(rs.getDouble(6));
            b.setDeliveryPrice(rs.getDouble(7));
            b.setRemarks(rs.getString(8));
            b.setOrderTypeId(rs.getInt(9));
            list.add(b);
        }
        ConnUtils.close(rs,ps);
        return list;
    }
}
